<?php
class Admin_SettingController extends Zend_Controller_Action {
	public function init() {
		$layoutPath = APPLICATION_PATH . '/templates/admin';
		$options = array (
				'layout' => 'index',
				'layoutPath' => $layoutPath 
		);
		Zend_Layout::startMvc ( $options );
	}
	public function indexAction() {
		$this->view->Settings = Zend_Registry::get ( 'Setting' );
	}
	public function editAction() {
		$requests = $this->getRequest ()->getParams ();
		$System = Systems::getById ( $requests ['id'] );
		if ($System) {
			$this->view->System = $System;
			
			if ($this->getRequest ()->isPost ()) {
				$System->merge ( $requests );
				$System->save ();
				$this->view->message = "Update setting successfull";
			}
		} else {
			$this->_redirect ( $this->url ( array (
					'controller' => 'index',
					'action' => 'index',
					'module' => 'admin' 
			) ) );
		}
	}
	public function deleteAction() {
		// action body
	}
	public function createAction() {
		if ($this->getRequest ()->isPost ()) {
			$requests = $this->getRequest ()->getParams ();
			$System = new Systems ();
			$System->merge ( $requests );
			if ($System->trySave ()) {
				$this->view->message = "Create setting successfull";
			} else {
				$this->view->request = $requests;
			}
		}
	}
	public function generalcodeAction() {
		if ($this->getRequest ()->isPost ()) {
			$requests = $this->getRequest ()->getParams ();
			try {
				if ($requests ['type'] == 1) {
					// print_r($requests);
					Doctrine_Core::generateModelsFromDb ( APPLICATION_PATH . '/models', array (
					'doctrine'
							), array (
							'generateTableClasses' => false
							) );
							Doctrine_Core::generateYamlFromModels ( 'schema.yml', APPLICATION_PATH . '/models' );
				} elseif ($requests ['type'] == 2) {
					Doctrine_Core::generateYamlFromModels ( 'schema.yml', APPLICATION_PATH . '/models' );
				}
				$error = self::_checkForm ( $requests );
				if (count ( $error ) == 0) {
					// write INI config file
					try {
						// define configuration object
						$config = new Zend_Config ( array (), true );
						$config->doctrine = array ();
						$config->doctrine->adapter = $requests ['adapter'];
	
						$config->doctrine->host = $requests ['host'];
						$config->doctrine->username = $requests ['username'];
						$config->doctrine->password = (empty ( $requests ['password'] )) ? '' : $requests ['password'];
						$config->doctrine->dbname = $requests ['database'];
						// print_r(APPLICATION_PATH .
						// '/configs/doctrine.ini');die();
						$writer = new Zend_Config_Writer_Ini ();
						$writer->write ( APPLICATION_PATH . '/configs/doctrine.ini', $config );
						echo 'Configuration successfully written to file.';
					} catch ( Exception $e ) {
						die ( 'ERROR: ' . $e->getMessage () );
					}
				} else {
					$this->view->error = $error;
				}
			} catch ( Exception $e ) {
				print_r ( $e->getMessage () );
				die ();
			}
		}
	}
	private function _checkForm($request) {
		$error = array ();
		if (empty ( $request ['adapter'] )) {
			$error ['adapter'] = 'Adapter isn\'t empty';
		}
		if ($request ['type'] == 2) {
			if (empty ( $request ['schema'] )) {
				$error ['schema'] = 'Schema isn\'t empty';
			}
		}
		if (empty ( $request ['host'] )) {
			$error ['host'] = 'Host isn\'t empty';
		}
		if (empty ( $request ['username'] )) {
			$error ['username'] = 'Username isn\'t empty';
		}
		if (empty ( $request ['database'] )) {
			$error ['database'] = 'Database isn\'t empty';
		}
		return $error;
	}
	public function loginAction(){
		
	}
}







